﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;
using ScrumTable.Common.Collections;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Config.UserManagement;
using ScrumTable.UI.ViewModel.Controller;
using Microsoft.Practices.Unity;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Extension;

#endregion

namespace ScrumTable.UI.ViewModel.View
{
    /// <summary>
    /// Description of the class
    /// </summary>
    public class MemberSettingViewModel : ViewModelBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private ScrumViewModelData _scrumData;
        private ICollectionView _teamMemberCollectionView;
        private ICollectionView _teamMemberCollectionViewFiltered;
        private ObservableCollection<TeamMember> _teamMembers;
        private ObservableCollection<TeamMember> _teamMembersFiltered;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------  

        /// <summary>
        /// Gets or sets the scrum data.
        /// </summary>
        /// <value>The scrum data.</value>
        [Dependency]
        public ScrumViewModelData ScrumData
        {
            get { return _scrumData; }
            set
            {
                _scrumData = value; 
                _scrumData.PropertyChanged +=  OnScrumDataPropertyChanged;

              
            }
        }

        public bool InWizardMode
        {
            get { return NaviController.CurrentTarget.IsWizardTarget(); }
        }


        public class TeamMember
        {
            public MemberViewModel Member { get; set; }
            public string TeamName { get; set; }

        }

        public ObservableCollection<TeamMember> TeamMembers
        {
            get
            {
                if(ScrumData.CurrentProject==null)
                {
                    return null;
                }

                var members = new List<TeamMember>();

                foreach (var member in ScrumData.CurrentProject.Members)
                {
                    if (!ScrumData.AccountManager.ContainsHiddenMember(member.Name))
                    {
                        if (member.AssignedTeams.IsEmpty())
                        {
                            members.Add(new TeamMember { TeamName = string.Empty, Member = new MemberViewModel(member) });
                        }
                        else
                        {
                            member.AssignedTeams.ForEach(
                                team => members.Add(new TeamMember {TeamName = team.Name, Member = new MemberViewModel(member)}));
                        }
                    }
                }

                if (_teamMembers == null)
                {
                    _teamMembers = new ObservableCollection<TeamMember>(members);
                    _teamMemberCollectionView = CollectionViewSource.GetDefaultView(_teamMembers);
                    _teamMemberCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("TeamName"));
                    _teamMemberCollectionView.SortDescriptions.Add(new SortDescription("TeamName", ListSortDirection.Ascending));
                }
                else
                {
                    _teamMembers.UnionToLeft(members);
                }

                return _teamMembers;
            }
        }
        public ObservableCollection<TeamMember> MembersFiltered
        {
            get
            {
                if(ScrumData.CurrentProject==null)
                {
                    return null;
                }
                //var members = from x in ScrumData.CurrentProject.Members where ScrumData.AccountManager.ContainsHiddenMember(x.Name) select new MemberViewModel(x);
                
                var members = new List<TeamMember>();

                foreach (var member in ScrumData.CurrentProject.Members)
                {
                    if (ScrumData.AccountManager.ContainsHiddenMember(member.Name))
                    {
                        if (member.AssignedTeams.IsEmpty())
                        {
                            members.Add(new TeamMember { TeamName = string.Empty, Member = new MemberViewModel(member) });
                        }
                        else
                        {
                            member.AssignedTeams.ForEach(
                                team => members.Add(new TeamMember { TeamName = team.Name, Member = new MemberViewModel(member) }));
                        }
                    }
                }

                if (_teamMembersFiltered == null)
                {
                    _teamMembersFiltered = new ObservableCollection<TeamMember>(members);
                    _teamMemberCollectionViewFiltered = CollectionViewSource.GetDefaultView(_teamMembersFiltered);
                    _teamMemberCollectionViewFiltered.GroupDescriptions.Add(new PropertyGroupDescription("TeamName"));
                    _teamMemberCollectionViewFiltered.SortDescriptions.Add(new SortDescription("TeamName", ListSortDirection.Ascending));
                }
                else
                {
                    _teamMembersFiltered.UnionToLeft(new List<TeamMember>(members));
                }
                return _teamMembersFiltered;
            }
        }

      
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Adds a member to the hidden list.
        /// </summary>
        /// <param name="name">The name.</param>
        public void HideMember(string name)
        {
            ScrumData.HideMember(name);
            SendPropertyChanged("MembersFiltered");
        }

        /// <summary>
        /// Removes a member from the hidden list.
        /// </summary>
        /// <param name="name">The name.</param>
        public void ShowMember(string name)
        {
            ScrumData.ShowMember(name);
            SendPropertyChanged("MembersFiltered");
        }

        protected override void ReleaseManagedResources()
        {
            base.ReleaseManagedResources();
            _scrumData.PropertyChanged -= OnScrumDataPropertyChanged;
            if (_teamMemberCollectionView != null)
            {
                _teamMemberCollectionView.GroupDescriptions.Clear();
                _teamMemberCollectionView = null;
            }

            ReleaseTeamMemberCollectionViewFiltered();
        }

        private void ReleaseTeamMemberCollectionViewFiltered()
        {
            if (_teamMemberCollectionViewFiltered != null)
            {
                _teamMemberCollectionViewFiltered.GroupDescriptions.Clear();
                _teamMemberCollectionViewFiltered = null;
                _teamMembersFiltered = null;
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------


        private void OnScrumDataPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "DataContext")
            {
                ReleaseTeamMemberCollectionViewFiltered();
            }
            if (e.PropertyName == "CurrentMembers" || e.PropertyName == "CurrentProject" || e.PropertyName == "CurrentIteration" || e.PropertyName == "DataContext")
            {
                SendPropertyChanged("MembersFiltered");
                SendPropertyChanged("TeamMembers");
            }
        }

        #endregion
    }
}